<?php

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!ispost())
{
    exit();
}
if (!isset($_SESSION['crtime']))
{
    $_SESSION['crtime'] = systime();
}
else if ($_SESSION['crtime'] == systime())
{
    exit();
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$betname_sb = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
if (20 < $gametype)
{
    $_8hFlag = true;
}
else
{
    $_8hFlag = false;
}
require_once( "class_sb/Bet_sb.php" );
require_once( "class_sb/RateConfig_sb.php" );
require_once( "class_sb/SRate_sb.php" );
require_once( "class_sb/MyLotto_sb.php" );
require_once( "class_sb/Amounts_sb.php" );
include_once( "class_sb/class.lottosb.php" );
$lottosb = new lottosb($gametype);
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
$RateConfig_sb = new RateConfig_sb($gametype);
$SRate_sb = new SRate_sb($gametype);
$Amounts_sb = new Amounts_sb($gametype);
$MyLotto_sb = new MyLotto_sb($gametype);
$Bet_sb = new Bet_sb($gametype);
$amountField = "balance";

$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$betmin = intval($conf->get("betmin_{$tbl}"));
$betmin2face = intval($conf->get("betmin2face_{$tbl}"));
if ($betmin2face <= 0)
{
    $betmin2face = $betmin;
}
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
else if (1 < $curruser['acl'])
{
    $ui->showErrorPage("非会员无法下注");
    exit();
}
$disk = intval(getvar("disk"));
if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] != 1)
{
    $ui->showErrorPage("盘类错误");
    exit();
}
unset($_POST['disk']);
$diskStr = $RateConfig_sb->diskArr[$disk];
$diskStr2 = $g_disk_name[$disk];
if ($bettypegroup == "" || !isset($RateConfig_sb->playGroupArr[$bettypegroup]))
{
    $ui->showErrorPage("不正确的玩法页面");
    exit();
}
if (intval(getvar("qFast")))
{
    $qFast = "1";
}
else
{
    $qFast = "";
}
if ($qFast)
{
    $qFastStr = "&qFast={$qFast}";
}
else
{
    $qFastStr = "";
}
include_once( "BetChk.php" );
$BetChk = new BetChk($today, $gametype);
if ($lotto = $BetChk->chkBetDate($today))
{
    $lotto_id = $lotto['id'];
    $paperOpen = $lotto['open'];
    $round = $lotto['round'];
    $showBetTable = array ();
    $rateArr = array ();
    $amountsDataArr = array ();
    $user_id = $SRate_sb->getAcl5ID($curruser['id'], $curruser['acl']);
    $u2 = get_user($curruser['prev_id'], $gametype);
    $u3 = get_user($u2['prev_id'], $gametype);
    $u4 = get_user($u3['prev_id'], $gametype);
    $u5 = get_user($u4['prev_id'], $gametype);
    $mpercent = $u2['mpercent'];
    $percent2 = $u2['percent0'];
    $percent3 = min($u3['percent1'], $u4['mpercent'] - $mpercent - $percent2);
    $percent4 = min($u4['percent1'], $u5['mpercent'] - $mpercent - $percent2 - $percent3);
    $upercent = 100 - $mpercent - $percent2 - $percent3 - $percent4;
    $fields = array ();
    $fields['percent2'] = $mpercent;
    $fields['percent3'] = $percent2;
    $fields['percent4'] = $percent3;
    $fields['percent5'] = $percent4;
    $fields['percent255'] = $upercent;
    $check_percent = $fields['percent255'];
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $recentNew = isset($_POST['recentNew']) ? intval($_POST['recentNew']) : 0;
    unset($_POST['recentNew']);
    unset($_POST['xyid']);
    $subid = 0 - 1;
    $bettype = 0 - 1;
    $nums = "";
	$usedamount=0;
    foreach ($_POST as $key => $val)
    {
        $betOKFlag = true;
        $val = intval($val);
        if (empty($val) || $val < 1)
        {
            $betOKFlag = false;
            continue;
        }
        $dataArr = explode("_", substr($key, 1));
        if (count($dataArr) != 4)
        {
            $betOKFlag = false;
            continue;
        }
        $subid = intval($dataArr[1]);
        $bettype = intval($dataArr[2]);
        $betSubIdx = $bettype;
		$nums = d2(intval($dataArr[3]));
        if (!isset($betname_sb[$betSubIdx]))
        {
            $betOKFlag = false;
            $timeTemp = rand(1000, 9999);
            $errMsg = "({$tbl})[betdo]No.{$timeTemp}-2 会员下注:不被许\可的玩法:id-{$bettype}, subid-{$subid}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$betSubIdx})");
            exit();
        }
		
        $numsStr = $nums;

        if (!$RateConfig_sb->checkBetNumber($bettype, $nums))
        {
            $betOKFlag = false;
            $timeTemp = rand(0, 9999);
            $errMsg = "({$tbl})[betdo]No.{$timeTemp}-5会员下注:不正确的内容:id-" . $bettype . ",num-" . $nums;
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }
		
        if (!isset($amountsDataArr[$betSubIdx]))
        {
            $amountsData = $Amounts_sb->getAmountsDataById($bettype, $curruser['id'], 0, 0);
            if ($amountsData == false)
            {
                $betOKFlag = false;
                $errMsg = "({$tbl})会员下注:无该玩法之下注限额资料:玩法id-" . $betSubIdx;
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "无限额资料", "xyid" => $xyid);
                continue;
            }
            $amountsDataArr[$betSubIdx] = $amountsData;
        }
        $amount1 = $amountsDataArr[$betSubIdx]['amount1'];
        $amount2 = $amountsDataArr[$betSubIdx]['amount2'];
        $amount1 = $amount1 ? $amount1 : "0";
        $amount2 = $amount2 ? $amount2 : "0";
        $betmin_tmp = in_array($bettype, $g_2face_sb) ? $betmin2face : $betmin;
        if ($val < $betmin_tmp)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能小於最低限额:" . $betmin_tmp, "xyid" => $xyid);
            continue;
        }
        if ($curruser[$amountField] < $val+ $usedamount)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过信用额度", "xyid" => $xyid);
            continue;
        }
        if ($curruser['usemaxamount'] && $curruser['maxamount'] < $val + $usedamount)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过本期限额", "xyid" => $xyid);
            continue;
        }
        if ($amount1 < $val)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过单注限额", "xyid" => $xyid);
            continue;
        }
        $ret = $Amounts_sb->check_Amount_255($lotto_id, $bettype, $nums, $val * $upercent / 100, $subid);
        if (count($ret))
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => implode(",", $ret) . " 总额度已满,无法下注.", "xyid" => $xyid);
            $errMsg = "({$tbl})会员下注:该玩法总额度已满:id- {$bettype}, {$numsStr}, {$subid}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        $tempRateBaseArr = $SRate_sb->getRates($lotto_id, $disk, $bettype, $subid);
        if ($tempRateBaseArr == false)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "无赔率资料", "xyid" => $xyid);
            $errMsg = "({$tbl})会员下注:无该玩法赔率资料:id-{$bettype}, subid-{$subid}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        if ($tempRateBaseArr2 = $SRate_sb->getRatesS($lotto_id, $bettype, $user_id, $subid))
        {
            $tempRateArr = array ();
            foreach ($tempRateBaseArr as $key2 => $val2)
            {
                $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
            }
            $rateArr[$betSubIdx] = $tempRateArr;
            $rate5 = $RateConfig_sb->getRateFromRateArr($bettype, $nums, $tempRateBaseArr2);
        }
        else
        {
            $rateArr[$betSubIdx] = $tempRateBaseArr;
            $rate5 = 0;
        }
        $rate = $RateConfig_sb->getRateFromRateArr($bettype, $nums, $rateArr[$betSubIdx]);
        if ($rate <= 1)
        {
            $betOKFlag = false;
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "赔率值错误", "xyid" => $xyid);
            $errMsg = "({$tbl})会员下注:赔率值错误:id-" . $bettype . ",num-" . $numsStr . ",subid-" . $subid;
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        $oldrate = getvar("rate{$bettype}v{$numsStr}v{$subid}");
        if ($recentNew == 0)
        {
            if ($oldrate != $rate)
            {
                $betOKFlag = false;
                $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "赔率已更新", "xyid" => $xyid);
                continue;
            }
        }
        if ($betOKFlag)
        {
            $fields['user_id'] = $curruser['id'];
            $fields['lotto_id'] = $lotto_id;
            $fields['bettype'] = $bettype;
            $fields['subid'] = $subid;
            $fields['numbers'] = is_array($nums) ? "'" . implode(",", $nums) . "'" : "'{$nums}'";
            $fields['amount'] = $val;
            $fields['rate'] = $rate;
            $fields['rate5'] = $rate5;
            $fields['payoff'] = $val * ( $rate - 1 );
            $fields['disk'] = $disk;
            $fields['line_nums'] =$fields['numbers'];// "'" . $RateConfig_sb->getBetContent($bettype, $nums) . "'";
            $fields['unitmoney'] = $val;
            $fields['rame'] = "0";
            $fields['State'] = "0";
            $fields['ip'] = "'" . getIP() . "'";
            $fields['back1'] = $Amounts_sb->getBackArr($bettype, $curruser['id'], $disk, 0);
            $fields['back2'] = $Amounts_sb->getBackArr($bettype, $u2['id'], $disk, 0);
            $fields['back3'] = $Amounts_sb->getBackArr($bettype, $u3['id'], $disk, 0);
            $fields['back4'] = $Amounts_sb->getBackArr($bettype, $u4['id'], $disk, 0);
            $fields['back5'] = $Amounts_sb->getBackArr($bettype, $u5['id'], $disk, 0);
            if ($u5['supe_account'] != "")
            {
                $rs = getdb("select id from k_user where acl=255 and account='" . $u5['supe_account'] . "'");
                $uid = $rs->f['id'];
                $rs->close();
                if ($uid != "")
                {
                    $fields['back6'] = $Amounts_sb->getBackArr($bettype, $uid, $disk, 0);
                }
                else
                {
                    $fields['back6'] = "0";
                }
            }
            else
            {
                $fields['back6'] = "0";
            }
            if ($fields['percent255'] < 0)
            {
                $ui->showErrorPage("系统参数严重错误！");
                exit();
            }
            $fields['r2'] = $val * $fields['percent3'] / 100;
            $fields['r3'] = $val * $fields['percent4'] / 100;
            $fields['r4'] = $val * $fields['percent5'] / 100;
            $fields['r6'] = $val * $fields['percent255'] / 100;
            $fields['createtime'] = systime();
            $fields['sessid'] = "'" . enBetSessid($fields) . "'";
            $_SESSION['crtime'] = $fields['createtime'];
            $numberamount = $Bet_sb->getSumNumberAmount($curruser['id'], $lotto_id, $bettype, $nums, $subid);
            if ($amount2 < $val + $numberamount)
            {
                $betOKFlag = false;
                $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过单项限额", "xyid" => $xyid);
                continue;
            }
            $insertFlag = $Bet_sb->dbInsert($fields);
            $insertId = "";
            if ($insertFlag == false)
            {
                $note = "<font color=\"red\">下注失败</font>";
            }
            else if ($oldrate != "" && $oldrate != $rate)
            {
                $note = "<font color=\"red\">赔率已更新</font>";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            else
            {
                $note = "";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            $showBetTable[] = array ("bettype" => $betname_sb[$betSubIdx], "nums" => $RateConfig_sb->getBetContent($bettype, $nums), "rate" => $rate, "betmoney" => $val, "betflag" => $insertFlag, "note" => $note, "xyid" => $xyid);
            if ($insertId !== "")
            {
                $lottosb->getBet_sb($insertId);
            }
			$usedamount+=$val;
        }
    }
    $ui->header();
    echo "<table class=\"tblMy3D\">";
    echo "<thead><tr class=\"bgImg_" . $gametype . "\"><th colspan=7>" . $tblName . " 第" . $round . "期 您下注的是：</th></tr>";
    echo "<tr class=\"bgClc_" . $gametype . "\">";
    echo "<th>下注类型</th><th>下注内容</th><th>盘类</th><th>赔率</th><th>下注金额</th><th>下注结果</th><th>说明</th></tr></thead>";
    echo "<tbody>";
    foreach ($showBetTable as $val)
    {
        if ($val['betflag'])
        {
            $tdMsg = "<font color=green>成功</font>";
            $classTemp = "td2";
        }
        else
        {
            $tdMsg = "失败";
            $classTemp = "red";
        }
        echo "<tr class=\"" . $classTemp . "\">";
        echo "<td>" . $val['bettype'] . "</td>";
		echo "<td>" . $val['nums'] . "</td>";
        echo "<td>" . $diskStr2 . "</td>";
        echo "<td>" . $val['rate'] . "</td>";
        echo "<td align=\"right\">" . $val['betmoney'] . "</td>";
        echo "<td>";
        echo $tdMsg;
        echo "</td>";
        echo "<td>" . $val['note'] . "</td>";
        echo "</tr>";
    }
    echo "</tbody>";
    $timebtnType = getvar("timebtnType") == "checkbox" ? "checkbox" : "text";
    $showSubid = intval(getvar("subid"));
    $rcbettype = intval(getvar("rcbettype"));
    echo "<tfoot><tr class=\"td6\" align=\"center\"><td colspan=\"7\">存档成功! 5秒後自动更新画面<img src=\"./image/loading.gif\" width=\"15\" height=\"15\" />, 或<input type=\"button\" value=\"回主页面\" onclick=\"javascript:myBetReload();\" class=\"colorBtn01 clrBrd_" . $gametype . "\" /></td></tr></tfoot>";
    echo "</table>";
    echo "<script>";
    echo "parent.leftFrame.location.href='op.php?op=member_sb&fp=account&gametype={$gametype}';";
    echo "setTimeout(\"window.top.mainFrame.location.replace('op.php?op=member_sb&fp=bet&palygroup={$bettypegroup}&timebtnType={$timebtnType}&gametype={$gametype}&disk={$disk}&xyid={$xyidGet}&subid={$subid}&rcbettype={$rcbettype}{$qFastStr}');\",5000);";
    echo "function myBetReload(){ window.top.mainFrame.location.replace('op.php?op=member_sb&fp=bet&palygroup={$bettypegroup}&timebtnType={$timebtnType}&gametype={$gametype}&disk={$disk}&xyid={$xyidGet}&subid={$subid}&rcbettype={$rcbettype}{$qFastStr}'); }";
    echo "</script>";
    $ui->footer();
}
?>